# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/topics/item-pipeline.html
from bson import binary
from PIL import Image
from items import buyItem,amazonItem
import urllib
from cStringIO import StringIO
class buy():
	def process_item(self,item,spider):
		update(item)
		return item		
			
def update(item):
	import pymongo
	dic=dict(item)

	tmpfile=urllib.urlopen(dic['img'])
	tmp=StringIO(tmpfile.read())
	im=Image.open(tmp)
	tu=im.size
	tm=im.resize((int(tu[0]*0.4),int(tu[1]*0.4)))
	newtmp=StringIO()
	tm.save(newtmp,'png')
	newtmp.seek(0)
	dic['imgdate']=binary.Binary(newtmp.read())
	del tm,newtmp,im,tmp,tmpfile
	tmppri=dic['price']
	if isinstance(tmppri, basestring) and tmppri.startswith('http'):
		tm=binary.Binary(urllib.urlopen(dic['price']).read())
		dic['pricepic']=tm
	conn=pymongo.Connection('mongodb://audoe:weihong@mongoc2.grandcloud.cn:10005/testtmp',slave_okay=True)
	db=conn.testtmp
	table=db.table
	table.insert(dic)
